﻿using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;

namespace Incubator.Controls
{
    public partial class QuizDisplay : System.Web.UI.UserControl
    {
        protected void Page_Init(object sender, EventArgs e)
        {
            if (Session["UserID"] == null || (int)Session["UserID"] == -1)
                Response.Redirect("main.aspx");


            if (Session["quizId"] != null)
            {
                LoadQuiz((int)Session["quizId"]);
            }
            else
            {
                lblError.Text = "quizId is not availble during Page_load";
                //no quiz to load??
                throw new Exception("There is no quiz currently available!");
            }
        }
        public void LoadQuiz(int quiz_id)
        {

            if (Session["questionNum"] == null)
            {//load first question

                webRef.Quiz q = Global.incuServ.GetQuiz(quiz_id);

                Session["questionNum"] = 1;
                int questionNum = 1;

                this.lblQuizName.Text = q.name;
                this.lblQustionNum.Text = questionNum.ToString() + " / " + q.questions.Length.ToString();
                this.Question1.LoadQuestion(q.questions[questionNum - 1]);

            }
            else
            {
                int questionNum = (int)Session["questionNum"];
                webRef.Quiz q = Global.incuServ.GetQuiz(quiz_id);

                this.lblQuizName.Text = q.name;
                this.lblQustionNum.Text = questionNum.ToString() + " / " + q.questions.Length.ToString();
                this.Question1.LoadQuestion(q.questions[questionNum - 1]);
           
            }
        }

        protected void btnSubmit_Click(object sender, EventArgs e)
        {

            //get answer
            string answer = this.Question1.getSelectedValue();
            if (answer == null || answer == "")
            {
                lblError.Text = "Please select an answer!";
                return;
            }

            webRef.Quiz q = Global.incuServ.GetQuiz((int)Session["quizId"]);
            //increment quesiton num
            int questionNum = (int)Session["questionNum"] + 1;
            Session["questionNum"] = questionNum ;

            webRef.User u = (webRef.User)Session["User"];
            webRef.Company c = Global.incuServ.GetCompany(u.company_id);
            Global.incuServ.AnswerQuestion(c, q, q.questions[questionNum - 2], answer);
            
            if (questionNum > q.questions.Length)
            {
                //TODO: quiz had ended, redirect to results
                Session["questionNum"] = null;
                Session["quizNum"] = null;
                Response.Redirect("QuizResult.aspx");
            }
            else
            {
                //change button text
                if (questionNum < q.questions.Length)
                    btnSubmit.Text = "Next";
                else
                    btnSubmit.Text = "Submit";

                this.lblQustionNum.Text = questionNum.ToString() + " / " + q.questions.Length.ToString();
                this.Question1.LoadQuestion(q.questions[questionNum - 1]);
           
                
            }
            
        }


    }
}